
part1 = 'gbj5SdkVmLhVH'
part2 = 'an5WazRnL2VGZjRXa'
alld = ('==' + part1 + part2).split('').reverse().join('')
var API_ENDPOINT = '';
if (window.location.href.indexOf("localhost") > 0 || window.location.href.indexOf("127.0.0.1") > 0 || window.location.href.indexOf("101") > 0){
    API_ENDPOINT = "";
}else{

    API_ENDPOINT = 'https://' + decodeURI(atob(alld)) + '/app/hainanhua/';

}

function sendToPolly(text, voice,picShow) {
    showPic = picShow
    var data = {
        w: text
    };
    $.ajax({
        url: API_ENDPOINT + "/deal?",
        type: 'GET',
        data: data,
        dataType:'json',
        success: readResponse,
        error: errorResponse,
    });


}

function errorResponse(response) {
    $('.error').text('出现错误').show();
}

function readResponse(response) {
    if(response.res == 'ok'){
       ssrc = API_ENDPOINT + "/speak?" + 'p=' + response.name
       var source = '<source src="' +  ssrc   + '" type="audio/mpeg"></source>' ;
        // var audio = '<div><audio id="audioPlayer" autoplay="true" controls>' + source + '</audio></div>' + '<div style="text-align:center">如果未自动播音请点击播放</div>';
        // $('.audio').html(audio);
        var player = document.getElementById("audioPlayer");
        $("#audioPlayer").attr("src",ssrc);

        $('#audioplayBoard').show();
        //
        player.load();
        player.play();

        
        canvasTxt = response.pho.replace(/([0-8])\)/g,'<sup>$1</sup>)&nbsp;').replace(/([a-z])([0-8]) /g,'$1<sup>$2</sup>&nbsp;')
        
        $('.result').html(canvasTxt).show();
        $('.result').show();

    }else{
        $('.error').text('查询出错').show();
        reset();
    }
}

function getQueryString(name) {
    if(window.location.href.indexOf("=") < 0){
            return null;
    }

    l=window.location.href.substr(window.location.href.indexOf("chineseWord=") + 12,window.location.href.length)
    if(l.indexOf("&") > 0){
        l=l.substr(0,l.indexOf("&") )
    }
    return decodeURIComponent(l);
}

　　







function queryServer(){
    var text = $('#ipa-text').val();
    if(text == ''){
       $('#searchHint').show();
       $('.result').hide();
       $('#audioplayBoard').hide(); 
    }else{
       $('#searchHint').hide();
       sendToPolly(text, null, true); 
    }
    
}




$(document).ready(function() {

    $('#ipa-text').on('change', function(e) {
        queryServer();
    });    

    $('#ipa-text').on('search', function(e) {
        queryServer();
    });    

    $("#ipa-text").click(function() {
        $(this).select();
    });

});






